<?php
define('IZ_ACCNAME','LOCAL');
@include('includes/classes.php');
session_start();
$queries = 0;
$numqs = 0;
$itemno = 1;
if($_GET['act'] == 'rss') {
  header("Content-Type: text/xml; charset=utf-8");
} else {
  header("Content-Type: text/html; charset=utf-8");
}
if(!isset($_SESSION['IZ_CALPATH'])) {
  $filename = $_SERVER['SCRIPT_FILENAME'];
  if(strpos($filename, 'ajax.php') !== false) {
    $_SESSION['IZ_CALPATH'] = substr($_SERVER['SCRIPT_FILENAME'],0,strlen($_SERVER['SCRIPT_FILENAME'])-17);
  } elseif(strpos($filename, 'functions_mini.php') !== false) {
    $_SESSION['IZ_CALPATH'] = substr($_SERVER['SCRIPT_FILENAME'],0,strlen($_SERVER['SCRIPT_FILENAME'])-27);
  } elseif(strpos($filename, 'functions.php') !== false) {
    $_SESSION['IZ_CALPATH'] = substr($_SERVER['SCRIPT_FILENAME'],0,strlen($_SERVER['SCRIPT_FILENAME'])-22);
  } elseif(strpos($filename, 'mini.php') !== false) {
    $_SESSION['IZ_CALPATH'] = substr($_SERVER['SCRIPT_FILENAME'],0,strlen($_SERVER['SCRIPT_FILENAME'])-8);
  } elseif(strpos($filename, 'm.php') !== false) {
    $_SESSION['IZ_CALPATH'] = substr($_SERVER['SCRIPT_FILENAME'],0,strlen($_SERVER['SCRIPT_FILENAME'])-5);
  } else {
    $_SESSION['IZ_CALPATH'] = substr($_SERVER['SCRIPT_FILENAME'],0,strlen($_SERVER['SCRIPT_FILENAME'])-9);
  }
}
include_once($_SESSION['IZ_CALPATH'].'includes/functions.php');
if(is_file($_SESSION['IZ_CALPATH'].'includes/data/settings.php')) {
  require_once($_SESSION['IZ_CALPATH'].'includes/vars.php');
  include_once($_SESSION['IZ_CALPATH'].'includes/classes.php');
  if(!isset($_SESSION['mobilechecked'])) {
    if(ismobilebrowser()) { // resource intensive so do only once per login
      header('Location: '.$_SESSION['IZ_CALURL'].'m.php');
    }
  }
  if($_SESSION['IZ_ALLOWCOMMENTS'] != '0') {
    include_once($_SESSION['IZ_CALPATH'].'includes/functions_comments.php');
  }
  if(isset($_POST['setusercust'])) {
    $fromkeys = explode("#",$_POST['fromloc']);
    foreach($fromkeys as $key) {
      list($key, $val) = explode("_",$key);
      $_GET[$key] = $val;
      $_REQUEST[$key] = $val;      
    }
  }
  $user = new user();
  if($_REQUEST['ajax']) {
    include($_SESSION['IZ_CALPATH'].'includes/ajax.php');
  } elseif(isset($_REQUEST['events']) || isset($_REQUEST['todo'])) {
    if(isset($_REQUEST['store']) || isset($_REQUEST['delseries']) || isset($_REQUEST['saveseries']) || isset($_REQUEST['delevent']) || isset($_REQUEST['deltodo']) || ($_REQUEST['act'] == 'addevent') || ($_POST['mass'] == 'Delete') || $_POST['masssave']) {
      if(($_SESSION['edit'] > 0) || ($_SESSION['delete'] > 0)) {
        include($_SESSION['IZ_CALPATH'].'includes/store.php');
      } else {
        if($_SESSION['authed'] == 1) {
          iz_error('&#35;212&#58; '.ERR_NOPERMISSION);
        } else {
          header("Location: ".$_SESSION['IZ_CALURL']."index.php?act=login&from=act-manage");
        }
      }
    } elseif($_POST['act'] == 'getical') {
      // handles ical export for mass events form
       foreach($_POST as $var => $id) {
          if((substr($var,0,4) == 'even') && ($var != 'events')) {
             $ids[] = $id;  
          }
       }
       if(count($ids) == 0) {
         iz_error(ERR_NOEVENTS);
       } else {
         getical('ids',implode("-",$ids));
       }
    } else {
      if($_SESSION['canview'] == 1) {
        include($_SESSION['IZ_CALPATH'].'includes/calendar.php');
      } else {
        if($_SESSION['authed'] == 1) {
          iz_error('&#35;213&#58; '.ERR_NOPERMISSION);
        } else {
          header("Location: ".$_SESSION['IZ_CALURL']."index.php?act=login&from=default");
        }
      }
    }
  } elseif(isset($_POST['comments'])) {
    include($_SESSION['IZ_CALPATH'].'includes/store.php');
  } elseif(isset($_REQUEST['act'])) {
     switch($_REQUEST['act']) {
       case 'admin':
         if($_SESSION['cpanel'] == 1) {
           if(isset($_REQUEST['setadmin'])) {
             if($_POST['setadmin'] == 'Restore') {
               include($_SESSION['IZ_CALPATH'].'includes/admin.php'); 
             } else {
               include($_SESSION['IZ_CALPATH'].'includes/store.php');
             }
           } else {
             include($_SESSION['IZ_CALPATH'].'includes/admin.php');
           }
         } else {
           if($_SESSION['authed'] == 1) {
             iz_error('&#35;214&#58; '.ERR_NOPERMISSION);
           } else {
             header("Location: ".$_SESSION['IZ_CALURL']."index.php?act=login&from=act-admin");
           }
         }
         break;
       case 'getical':
       case 'ical':
         if($_REQUEST['type'] == 'ids') {
           foreach($_POST as $var => $id) {
              if((substr($var,0,4) == 'even') && ($var != 'events')) {
                 $ids[] = $id;  
              }
           }
           if(count($ids) == 0) {
             iz_error(ERR_NOEVENTS);
           } else {
             getical('ids',implode("-",$ids));
           }
         } elseif(!empty($_REQUEST['type']) && !empty($_REQUEST['start'])) {
           getical($_REQUEST['type'],$_REQUEST['start']);
         } else {
           getical('all','all');
         }
         break;
       case 'importical':
         include($_SESSION['IZ_CALPATH'].'includes/store.php');
         break;
       case 'print':
         include($_SESSION['IZ_CALPATH'].'includes/print.php');
         break;
       case 'rss':
         getrss();
         break;
       case 'upgrade':
         include($_SESSION['IZ_CALPATH'].'includes/install.php');
         break;
       case 'submitreg':
         include($_SESSION['IZ_CALPATH'].'includes/store.php');
         break;
       case 'changepass':
         include($_SESSION['IZ_CALPATH'].'includes/store.php');
         break;
       case 'rpc':
         include($_SESSION['IZ_CALPATH'].'includes/rpc.php');
         break;
       default:
         if($_SESSION['canview'] == 1) {
           include($_SESSION['IZ_CALPATH'].'includes/calendar.php');
         } else {
           if($_SESSION['authed'] == 1) {
             iz_error('&#35;215&#58; '.ERR_NOPERMISSION);
           } else {
             header("Location: ".$_SESSION['IZ_CALURL']."index.php?act=login&from=default");
           }
         }
     }
  } elseif((isset($_REQUEST['todo'])) || (isset($_POST['store']))) {
    if(($_SESSION['edit'] > 0) || ($_SESSION['delete'] > 0)) {
      include($_SESSION['IZ_CALPATH'].'includes/store.php');
    }
  } else {
    if($_SESSION['canview'] == 1) {
      include($_SESSION['IZ_CALPATH'].'includes/calendar.php');
    } else {
      if($_SESSION['authed'] == 1) {
        iz_error('&#35;216&#58; '.ERR_NOPERMISSION);
      } else {
        header("Location: ".$_SESSION['IZ_CALURL']."index.php?act=login&from=default");
      }
    }
  }
} else {
  include($_SESSION['IZ_CALPATH'].'includes/install.php');
}
if(($_GET['debug']) || ($_SESSION['debug'])) {
  $_SESSION['debug'] = 1;
  include($_SESSION['IZ_CALPATH'].'includes/debug.php');
} elseif($_GET['debug'] == '0') {
  $_SESSION['debug'] = 0;
}
if(($_GET['v']) || ($_GET['debug']) || ($_SESSION['debug'] == 1)) {
  var_dump($user);
  echo '<b>SESSION: </b><br />'; var_dump($_SESSION); echo '<br><br>';
  echo '<b>POST: </b><br />'; var_dump($_POST); echo '<br><br>';
  echo '<b>GET: </b><br />'; var_dump($_GET); echo '<br><br>';
  echo '<b>REQUEST: </b><br />'; var_dump($_REQUEST); echo '<br><br>';
}
?>